package cn.jd.basic.util;

import cn.jd.basic.jwt.*;
import cn.jd.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
@Slf4j
public class LoginContext {

    public static Logininfo getLogininfo(HttpServletRequest request){
        // 获取请求头上的token
        String token = request.getHeader("token");
        // 判断token是否为空
        if(!StringUtils.isEmpty(token)){
            try {
                // 调用工具类获取公钥
                PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
                Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
                Logininfo logininfo = payload.getUserInfo().getLogininfo();
                if(logininfo== null){
                    log.info("用户未登录");
                    return null;
                }
                return logininfo;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}
